<?php

class Lib_Grid_Doctrine2 extends Bvb_Grid_Deploy_Table
{

    public function __construct(array $options = array())
    {
        parent::__construct($options);

        $view = new Zend_View();
        $view->setEncoding('UTF-8');
        $this->setView($view);
    }

    public function clearExport()
    {
        $this->setExport(array());
    }

    public function setQb($qb)
    {
        $em = \Lib\ResLoader::getResource('Doctrine2');
        $source = new Bvb_Grid_Source_Doctrine2($qb, $em);
        $this->setSource($source);
    }

    public function hideColumns(array $columns)
    {
        foreach ($columns as $column) {
            $this->updateColumn($column, array('hidden' => true));
        }
    }

    public function createForm(array $actions)
    {
        $form = new Bvb_Grid_Form();

        $possibleParams = array('add', 'edit', 'delete', 'file', 'addbutton');

        foreach ($possibleParams as $param) {
            if (in_array($param, $actions)) {
                switch ($param) {
                    case 'add':
                        $form->setAdd(true);
                        break;
                    case 'edit':
                        $form->setEdit(true);
                        break;
                    case 'delete':
                        $form->setDelete(true);
                        break;
                    case 'addbutton':
                        $form->setAddButton(true);
                        break;
                    case 'file':
                        $form->addElement('file', 'fileUrl', array('label' => 'File Upload:'));
                        $form->getElement('fileUrl')
                                ->addValidator('Size', array('0kB', '1000kB'))
                                ->setDestination('../public/media/upload/');
                        break;

                }
            }
        }

        $this->setForm($form);
    }

    public function setBeforeInsertAction($callback) {
         $this->listenEvent('crud.before_insert', $callback);
    }

    public function setBeforeUpdateAction($callback) {
        $this->listenEvent('crud.before_update', $callback);
    }
}
